package com.wash.shoes.controller.pc;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.dto.ShoeImgDto;
import com.wash.shoes.domain.dto.ShoeUpdateDto;
import com.wash.shoes.domain.dy.request.DyServiceUpdateRequest;
import com.wash.shoes.domain.vo.ShoeVo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.ShoePo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.request.ShoeQueryRequest;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.ShoeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ljg
 * @since 2024-08-28
 */
@Api(tags = {"PC-鞋子"})
@RestController
@RequestMapping({"/dev-api/shoe", "/shoe"})
public class ShoeController {

    @Autowired
    ShoeService shoeService;

    @Autowired
    OrderService orderService;

    @Autowired
    DyServiceUpdateRequest serviceUpdateRequest;

    @Autowired
    DyServiceUpdateRequest dyServiceUpdateRequest;

    @ApiOperation("获取鞋子列表")
    @PostMapping("/list")
    public R<Page<ShoeVo>> pageList(@RequestBody ShoeQueryRequest shoePo) {
        shoePo.setNeedPage(true);
        Page<ShoeVo> page = shoeService.pageList(shoePo);
        return R.ok(page);
    }

    @ApiOperation("导出鞋子列表为excel表格")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShoeQueryRequest factoryQueryRequest) {
        factoryQueryRequest.setNeedPage(false);
        factoryQueryRequest.validate();
        Page<ShoeVo> page = shoeService.pageList(factoryQueryRequest);
        List<ShoeVo> list = page.getRecords();
        //ShoeExportVo
        ExcelUtil<ShoeVo> util = new ExcelUtil<ShoeVo>(ShoeVo.class);
        util.exportExcel(response, list, "鞋子列表导出");
    }


    @ApiOperation("获取鞋子的详情用washcode")
    @GetMapping("/shoeDetailByWashCode")
    public R<ShoeVo> shoeDetailByWashCode(String washCode) {
        ShoeVo shoeVo = shoeService.shoeDetailByWashCode(washCode);
        return R.ok(shoeVo);
    }

    @ApiOperation("获取鞋子的详情用id")
    @GetMapping("/shoeDetailById")
    public R<ShoeVo> shoeDetailById(String id) {
        ShoeVo shoeVo = shoeService.shoeDetailById(id);
        return R.ok(shoeVo);
    }

    @ApiOperation("清洗完成,如果所有的鞋子都清洗完成，订单的状态为待出库，多个鞋子逗号分隔")
    @GetMapping("/shoeClearFinish")
    @Transactional
    public R<Boolean> shoeClearFinish(String... ids) {
        for (String id : ids) {
            ShoePo shoePo = shoeService.getById(id);
            // 所有的鞋子清洗完成 订单就为待出库
            List<ShoePo> shoePoList = shoeService.orderNotWashShoe(shoePo.getOrderId(), id);
            // 所有的鞋子清洗完成, 待出库
            if (shoePoList.isEmpty()) {
                OrderPo orderPo = orderService.getById(shoePo.getOrderId());
                //只有清洗中的订单才能修改成待质检
                if (OrderStatusEnum.CLEANING_IN_PROCESS.getStatus() == orderPo.getStatus()) {
                    orderPo.setCompletionTime(LocalDateTime.now());//设置清洗完成的时间
                    orderPo.setStatus(OrderStatusEnum.WAIT_FOR_QUALITY.getStatus());//清洗完成该成待质检 by jjz
                    // 订单代储库要通知抖音
                    serviceUpdateRequest.updateFullFill(orderPo, 2);
                    orderService.updateById(orderPo);
                }
            }
            // 鞋子清洗完成
            shoePo.setStatus(2);
            shoeService.updateById(shoePo);
        }
        return R.ok(true);
    }


    /**
     * @author lijianguo
     * @Date 01/09/2024
     * 修改鞋子的清洗类型
     **/
    @ApiOperation("updateShoeWashType")
    @PostMapping("/updateShoeWashType")
    @Transactional
    public R<Boolean> updateShoeWashType(@RequestBody ShoeUpdateDto shoeUpdateDto) {

        for (ShoeImgDto shoeImgDto : shoeUpdateDto.getShoeImgDtoList()) {
            ShoePo shoePo = shoeService.getById(shoeImgDto.getShoeId());
            //  更新订单的备注
            OrderPo orderPo = orderService.getById(shoePo.getOrderId());
            orderPo.setShopRemark(shoeImgDto.getShopRemark());
            orderService.updateById(orderPo);
            // 设置鞋子的清洗类型
            shoePo.setWashType(shoeImgDto.getWashType());
            shoePo.setPicture(shoeImgDto.getShoeImg());
            shoeService.updateById(shoePo);
        }

        OrderPo orderPo = orderService.getById(shoeUpdateDto.getOrderId());
        if (orderPo == null) {
            throw new WashException("订单不存在");
        }
        orderPo.setFittingImg(shoeUpdateDto.getFittingImg());
        orderService.updateById(orderPo);
        return R.ok(true);
    }


    @ApiOperation("反洗鞋子")
    @GetMapping("/returnWash")
    public R<OrderPo> returnWash(String id) {

        // 鞋子出库的时候反洗，反洗的
        ShoePo shoePo = shoeService.getById(id);
        shoePo.setStatus(1);
        shoePo.setExtendInfo("反洗");
        shoeService.updateById(shoePo);

        // 设置为清洗中
        OrderPo orderPo = orderService.getById(shoePo.getOrderId());
        // 清洗中的订单 抖音核销
        orderPo.setStatus(OrderStatusEnum.CLEANING_IN_PROCESS.getStatus());
        dyServiceUpdateRequest.updateFullFill(orderPo, 4);
        orderService.updateById(orderPo);
        return R.ok(orderPo);
    }

}

